{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import networkx as nx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [],
   "source": [
    "edges = pd.DataFrame()\n",
    "edges['sources'] = [1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 5]\n",
    "edges['targets'] = [2, 4, 5, 3, 1, 2, 5, 1, 5, 1, 3, 4]\n",
    "edges['weights'] = [1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(1, 3), (2, 2), (4, 2), (5, 3), (3, 2)]\n"
     ]
    }
   ],
   "source": [
    "G = nx.from_pandas_edgelist(edges,source='sources',target='targets',edge_attr='weights')\n",
    "# degree\n",
    "print(nx.degree(G))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{1, 2, 3, 4, 5}]\n"
     ]
    }
   ],
   "source": [
    "# 连通分量\n",
    "print(list(nx.connected_components(G)))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "# 图直径\n",
    "print(nx.diameter(G))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1: 0.75, 2: 0.5, 4: 0.5, 5: 0.75, 3: 0.5}\n"
     ]
    }
   ],
   "source": [
    "# 度中心性\n",
    "print(nx.degree_centrality(G))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "{1: 0.529898889076173,\n 2: 0.35775191431708964,\n 4: 0.4271316779596083,\n 5: 0.5298988890761731,\n 3: 0.35775191431708964}"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 特征向量中心性\n",
    "nx.eigenvector_centrality(G)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "{1: 0.25, 2: 0.08333333333333333, 4: 0.0, 5: 0.25, 3: 0.08333333333333333}"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# betweeness\n",
    "nx.betweenness_centrality(G)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "data": {
      "text/plain": "{1: 0.8,\n 2: 0.6666666666666666,\n 4: 0.6666666666666666,\n 5: 0.8,\n 3: 0.6666666666666666}"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# closeness\n",
    "nx.closeness_centrality(G)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "data": {
      "text/plain": "{1: 0.24369622576677993,\n 2: 0.1722562971205864,\n 4: 0.16809495422526696,\n 5: 0.24369622576677993,\n 3: 0.1722562971205864}"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# pagerank\n",
    "nx.pagerank(G)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\ThinkPad\\AppData\\Roaming\\Python\\Python39\\site-packages\\networkx\\algorithms\\link_analysis\\hits_alg.py:78: FutureWarning: adjacency_matrix will return a scipy.sparse array instead of a matrix in Networkx 3.0.\n",
      "  A = nx.adjacency_matrix(G, nodelist=list(G), dtype=float)\n"
     ]
    },
    {
     "data": {
      "text/plain": "({1: 0.24059715204600776,\n  2: 0.162434564716677,\n  4: 0.19393656647463042,\n  5: 0.24059715204600784,\n  3: 0.16243456471667692},\n {1: 0.24059715204600787,\n  2: 0.16243456471667692,\n  4: 0.1939365664746304,\n  5: 0.24059715204600773,\n  3: 0.16243456471667703})"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#HITS\n",
    "nx.hits(G)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}